package com.cskaoyan.service;

import com.cskaoyan.bean.PO.MarketBrand;
import com.cskaoyan.bean.PO.MarketBrandExample;
import com.cskaoyan.bean.common.BasePageInfo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.mapper.MarketBrandMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @Description:
 * @author:刘杰
 * @create: 2022-08-14 10:45
 */
@Service
public class AdminBrandServiceImpl implements AdminBrandService{

    @Autowired
    MarketBrandMapper marketBrandMapper;

    @Override
    public CommonData list(Integer id, String name, BasePageInfo info) {
        MarketBrandExample marketBrandExample = new MarketBrandExample();
        MarketBrandExample.Criteria criteria = marketBrandExample.createCriteria();

        if(info.getSort()!=null&&info.getOrder()!=null){
            marketBrandExample.setOrderByClause(info.getSort() + " " + info.getOrder());
        }
        if (id != null && !"".equals(id)) {
            criteria.andIdEqualTo(id);
        }
        if (name != null && !"".equals(name)) {
            criteria.andNameLike("%" + name + "%");
        }

        criteria.andDeletedEqualTo(false);
        PageHelper.startPage(info.getPage(),info.getLimit());
        List<MarketBrand> marketBrands = marketBrandMapper.selectByExample(marketBrandExample);
        PageInfo<MarketBrand> PageInfo = new PageInfo<>(marketBrands);
        CommonData data = CommonData.data(PageInfo);
        return data;

    }

    @Override
    public MarketBrand update(MarketBrand marketBrand) {
        marketBrand.setUpdateTime(new Date());
        marketBrandMapper.updateByPrimaryKeySelective(marketBrand);
        return marketBrand;
    }

    @Override
    public void delete(MarketBrand marketBrand) {

        MarketBrand marketBrand1 = new MarketBrand();
        marketBrand1.setId(marketBrand.getId());
        marketBrand1.setDeleted(true);
        marketBrand1.setUpdateTime(new Date());
        marketBrandMapper.updateByPrimaryKeySelective(marketBrand1);
    }

    @Override
    public MarketBrand create(MarketBrand marketBrand) {
        //创建marketBrand并赋值
        MarketBrand marketBrand1 = new MarketBrand();
        Date addTime = new Date();
        marketBrand1.setAddTime(addTime);
        marketBrand1.setUpdateTime(addTime);
        marketBrand1.setDesc(marketBrand.getDesc());
        marketBrand1.setFloorPrice(marketBrand.getFloorPrice());
        marketBrand1.setName(marketBrand.getName());
        marketBrand1.setPicUrl(marketBrand.getPicUrl());

        marketBrandMapper.insertSelective(marketBrand1);
        return marketBrand;
    }

}
